import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/multi_area_ring_chart_painter.dart';
import 'package:x3_charts_core/model/general_chart_model.dart';

///**
///* @author hrongyong
///* @date   3/2/21
///* @email hrongyong@gmail.com
///* Description: 多区域环形图
///               使用的是GeneralChartModel作为数据源，不再另外新增
///*/
class MultiAreaRingChartWidget extends StatefulWidget {
  final List<GeneralChartModel> chartList;
  final Color backgroundColor;

  ///圆环上是否显示占比
  final bool displayPercentageToCircle;

  ///是否显示圆环和文字之间的刻度线
  final bool showTickMark;

  MultiAreaRingChartWidget({
    Key key,
    @required this.chartList,
    this.backgroundColor = Colors.white,
    this.displayPercentageToCircle = false,
    this.showTickMark = true,
  });

  @override
  _MultiAreaRingChartWidgetState createState() => _MultiAreaRingChartWidgetState();
}

class _MultiAreaRingChartWidgetState extends State<MultiAreaRingChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return CustomPaint(
            size: Size(constraints.maxWidth, constraints.maxHeight),
            painter: MultiAreaRingChartPainter(
              repaint: _controller,
              chartList: widget.chartList,
              backgroundColor: widget.backgroundColor,
              displayPercentageToCircle: widget.displayPercentageToCircle,
              showTickMark: widget.showTickMark,
            ),
          );
        },
      ),
    );
  }
}
